package com.tianze.tzkf.function.aboutUs;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianze.tzkf.R;
import com.tianze.tzkf.bean.GetVersionBean;
import com.tianze.tzkf.function.leadRecord.LeadRecordPayAdapter;
import com.tianze.tzkf.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Zcyang on 2018/12/29.
 */
@SuppressLint("ValidFragment")
public class AboutUsUpdateDialog extends DialogFragment {


    @BindView(R.id.tv_update_dialog_title)
    TextView tvUpdateDialogTitle;
    @BindView(R.id.iv_update_dialog_close)
    ImageView ivUpdateDialogClose;
    @BindView(R.id.tx_update_dialog_msg)
    TextView tx_update_dialog_msg;
    @BindView(R.id.tv_update_dialog_start)
    TextView tvUpdateDialogStart;
    Unbinder unbinder;

    GetVersionBean.DataBean dataBean;

    @SuppressLint("ValidFragment")
    public AboutUsUpdateDialog(Activity activity, String str, GetVersionBean.DataBean dataBean) {
        this.dataBean = dataBean;
    }

    public static AboutUsUpdateDialog newInstance(Activity activity, String str ,GetVersionBean.DataBean dataBean) {

        Bundle args = new Bundle();
        AboutUsUpdateDialog fragment = new AboutUsUpdateDialog(activity, str,dataBean);
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_fragment_update, container);
        //        slideToUp(view);
        unbinder = ButterKnife.bind(this, view);
        return view;

    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.dimAmount = 0.50f;
        params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }


    @Override
    public void onResume() {
        super.onResume();
        getPayList();
    }

    private void getPayList() {

        bindList();

    }

    private void bindList() {
//        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getActivity());
//        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        mLinearLayoutManager.setAutoMeasureEnabled(true);
//        rvUpdateDialogMsg.setLayoutManager(mLinearLayoutManager);
//        rvUpdateDialogMsg.setHasFixedSize(true);
//        rvUpdateDialogMsg.setNestedScrollingEnabled(false);
//        rvUpdateDialogMsg.setFocusable(true);

//        List<String> list = new ArrayList<>();
//
//        for (int i = 0; i < 10; i++) {
//
//            list.add("123");
//        }

//        AboutUsUpdateMsgAdapter mLeadRecordPayAdapter = new AboutUsUpdateMsgAdapter(R.layout.item_update_msg, list);
//        rvUpdateDialogMsg.setAdapter(mLeadRecordPayAdapter);
//        mLeadRecordPayAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                ToastUtils.showToast(getActivity(), "点击了" + position);
//            }
//        });


        if (dataBean.getUpdateVersion()!=null){
            tvUpdateDialogTitle.setText("新版本说明");
            tx_update_dialog_msg.setText(dataBean.getUpdateVersion().getNote()+"");
            tvUpdateDialogStart.setVisibility(View.VISIBLE);
        }else {
            if (dataBean.getCurrentVersion() != null){
                tvUpdateDialogTitle.setText("当前版本说明");
                tx_update_dialog_msg.setText(dataBean.getCurrentVersion().getNote()+"");
                tvUpdateDialogStart.setVisibility(View.GONE);
            }
        }


    }

    private void slideToUp(View view) {

        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f, Animation.RELATIVE_TO_SELF, 0.0f);

        slide.setDuration(300);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        view.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.iv_update_dialog_close, R.id.tv_update_dialog_start})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_update_dialog_close:
                dismiss();
                break;
            case R.id.tv_update_dialog_start:
                dismiss();
                break;
        }
    }
}
